#!/bin/bash
###
 # @Author: didiplus
 # @Date: 2025-01-27 14:26:12
 # @LastEditors: didiplus
 # @Description: 
 # @LastEditTime: 2025-01-27 16:21:23
### 


green='\033[32m' #绿色
white='\033[0m' #白色
yellow='\033[33m' #黄色

ip_address() {
ipv4_address=$(curl -s ipv4.ip.sb)
ipv6_address=$(curl -s --max-time 1 ipv6.ip.sb)
}

output_status() {
	output=$(awk 'BEGIN { rx_total = 0; tx_total = 0 }
		NR > 2 { rx_total += $2; tx_total += $10 }
		END {
			rx_units = "Bytes";
			tx_units = "Bytes";
			if (rx_total > 1024) { rx_total /= 1024; rx_units = "KB"; }
			if (rx_total > 1024) { rx_total /= 1024; rx_units = "MB"; }
			if (rx_total > 1024) { rx_total /= 1024; rx_units = "GB"; }

			if (tx_total > 1024) { tx_total /= 1024; tx_units = "KB"; }
			if (tx_total > 1024) { tx_total /= 1024; tx_units = "MB"; }
			if (tx_total > 1024) { tx_total /= 1024; tx_units = "GB"; }

			printf("总接收:       %.2f %s\n总发送:       %.2f %s\n", rx_total, rx_units, tx_total, tx_units);
		}' /proc/net/dev)

}

current_timezone() {
	if grep -q 'Alpine' /etc/issue; then
	   date +"%Z %z"
	else
	   timedatectl | grep "Time zone" | awk '{print $3}'
	fi

}

linux_ps() {
	ip_address

	local cpu_info=$(lscpu | awk -F': +' '/Model name:/ {print $2; exit}')

	local cpu_usage_percent=$(awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else printf "%.0f\n", (($2+$4-u1) * 100 / (t-t1))}' \
		<(grep 'cpu ' /proc/stat) <(sleep 1; grep 'cpu ' /proc/stat))

	local cpu_cores=$(nproc)

	local cpu_freq=$(cat /proc/cpuinfo | grep "MHz" | head -n 1 | awk '{printf "%.1f GHz\n", $4/1000}')

	local mem_info=$(free -b | awk 'NR==2{printf "%.2f/%.2f MB (%.2f%%)", $3/1024/1024, $2/1024/1024, $3*100/$2}')

	local disk_info=$(df -h | awk '$NF=="/"{printf "%s/%s (%s)", $3, $2, $5}')

	local ipinfo=$(curl -s ipinfo.io)
	local country=$(echo "$ipinfo" | grep 'country' | awk -F': ' '{print $2}' | tr -d '",')
	local city=$(echo "$ipinfo" | grep 'city' | awk -F': ' '{print $2}' | tr -d '",')
	local isp_info=$(echo "$ipinfo" | grep 'org' | awk -F': ' '{print $2}' | tr -d '",')

	local load=$(uptime | awk '{print $(NF-2), $(NF-1), $NF}')
	local dns_addresses=$(awk '/^nameserver/{printf "%s ", $2} END {print ""}' /etc/resolv.conf)


	local cpu_arch=$(uname -m)

	local hostname=$(uname -n)

	local kernel_version=$(uname -r)

	local congestion_algorithm=$(sysctl -n net.ipv4.tcp_congestion_control)
	local queue_algorithm=$(sysctl -n net.core.default_qdisc)

	local os_info=$(grep PRETTY_NAME /etc/os-release | cut -d '=' -f2 | tr -d '"')

	output_status

	local current_time=$(date "+%Y-%m-%d %I:%M %p")


	local swap_info=$(free -m | awk 'NR==3{used=$3; total=$2; if (total == 0) {percentage=0} else {percentage=used*100/total}; printf "%dMB/%dMB (%d%%)", used, total, percentage}')

	local runtime=$(cat /proc/uptime | awk -F. '{run_days=int($1 / 86400);run_hours=int(($1 % 86400) / 3600);run_minutes=int(($1 % 3600) / 60); if (run_days > 0) printf("%d天 ", run_days); if (run_hours > 0) printf("%d时 ", run_hours); printf("%d分\n", run_minutes)}')

	local timezone=$(current_timezone)

	local login_num=$(who | wc -l)


	echo ""
	echo -e "系统信息查询"
	echo -e "${yellow}-------------"
	echo -e "${yellow}主机名:       ${green}$hostname"
	echo -e "${yellow}系统版本:     ${green}$os_info"
	echo -e "${yellow}Linux版本:    ${green}$kernel_version"
	echo -e "${yellow}-------------"
	echo -e "${yellow}CPU架构:      ${green}$cpu_arch"
	echo -e "${yellow}CPU型号:      ${green}$cpu_info"
	echo -e "${yellow}CPU核心数:    ${green}$cpu_cores"
	echo -e "${yellow}CPU频率:      ${green}$cpu_freq"
	echo -e "${yellow}-------------"
	echo -e "${yellow}CPU占用:      ${green}$cpu_usage_percent%"
	echo -e "${yellow}系统负载:     ${green}$load"
	echo -e "${yellow}物理内存:     ${green}$mem_info"
	echo -e "${yellow}虚拟内存:     ${green}$swap_info"
	echo -e "${yellow}硬盘占用:     ${green}$disk_info"
	echo -e "${yellow}-------------"
	echo -e "${yellow}$output"
	echo -e "${yellow}-------------"
	echo -e "${yellow}网络算法:     ${green}$congestion_algorithm $queue_algorithm"
	echo -e "${yellow}-------------"
	echo -e "${yellow}运营商:       ${green}$isp_info"
	if [ -n "$ipv4_address" ]; then
		echo -e "${yellow}IPv4地址:     ${green}$ipv4_address"
	fi

	if [ -n "$ipv6_address" ]; then
		echo -e "${yellow}IPv6地址:     ${green}$ipv6_address"
	fi
	echo -e "${yellow}DNS地址:      ${green}$dns_addresses"
	echo -e "${yellow}地理位置:     ${green}$country $city"
	echo -e "${yellow}系统时间:     ${green}$timezone $current_time"
	echo -e "${yellow}-------------"
	echo -e "${yellow}运行时长:     ${green}$runtime"
    echo -e "${yellow}登录用户数:     ${green}$login_num${white}"
	echo



}

linux_ps